﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Se.Data
{
    /// <summary>
    /// SQL条件
    /// </summary>
    public struct SQLFilter
    {
        #region 属性

        /// <summary>
        /// 数据类型
        /// </summary>
        public DBFilterType DataType { get; private set; }
        /// <summary>
        /// 数据运算符
        /// </summary>
        public DBFilterOperator Operator { get; private set; }
        /// <summary>
        /// 参数的值
        /// </summary>
        public string ParamValue { get; private set; }
        /// <summary>
        /// 参数的列名
        /// </summary>
        public string ParamName { get; private set; }


        #endregion


        /// <summary>
        /// 获取SQL字符串
        /// </summary>
        /// <returns></returns>
        public string ToSQLFilterString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" ");
            sb.Append("[").Append(this.ParamName).Append("]");
            sb.Append(GetOperatorString());
            switch (this.DataType)
            {
                case DBFilterType.DateTime:
                case DBFilterType.String:
                    sb.Append("'@").Append(this.ParamValue).Append("'");
                    break;
                case DBFilterType.Float:
                case DBFilterType.Int:
                    sb.Append("@").Append(this.ParamValue);
                    break;
                default:
                    throw new Exception("Unknown SQLDataType!");
            }

            return sb.ToString();
        }

        private string GetOperatorString()
        {
            string operat = string.Empty;
            switch (this.Operator)
            {
                case DBFilterOperator.Bigger:
                    operat = ">";
                    break;
                case DBFilterOperator.BiggerAndEqual:
                    operat = ">=";
                    break;
                case DBFilterOperator.Equal:
                    operat = "=";
                    break;
                case DBFilterOperator.Smaller:
                    operat = "<";
                    break;
                case DBFilterOperator.SmallerAndEqual:
                    operat = "<=";
                    break;
            }

            return operat;
        }

    }
}
